/* R. J. Cano, Jan 27 2014 */

/* Reserved globals */
stdoutIt=1;
lastDiffCount=0;
maxPrimeIndex=144634238; /* By calling gp with -f -q -s 10240000000 -p 3004031629 ## For example */

/*
 * Well this recall a little the coding style for Java,
 * in fact it was the way to get'em working these globals
 * with gp2c;
 * 
 */

set_stdoutIt(x)={stdoutIt=x}
set_lastDiffCount(x)={lastDiffCount=x}
set_maxPrimeIndex(x)={maxPrimeIndex=x}

get_stdoutIt()={stdoutIt}
get_lastDiffCount()={lastDiffCount}
get_maxPrimeIndex()={maxPrimeIndex}

/**/

/* Variation 0 is for the b-file with zeros, and variation 1 the originally proposed b-file */
sequencer(initial=1,final=1000000,lastpartialsum=0, variation=0)={my(u=lastpartialsum);for(n=initial,final,k=variation;u+=prime(n);while(!(isprime(u+k) || isprime(u-k)),k++);print(u" "k))}
vectmax(v,r=#v)={my(ans=1,U=min(r,#v));for(j=1,U,if(v[j]>v[ans],ans=j));ans}
vectfrq(v,k=vectmax(v))= {my(ans=0);for(j=1,#v,if(v[k]==v[j],ans+=1));ans}
vectmaxDiff(v1,v2,r=min(#v1,#v2))={my(thisDiffCount=0,ans0,ans1=1,ans2=1,U=min(#v1,min(#v2,r)));for(j=1,U,if(v1[j]>v1[ans1],ans1=j);if(v2[j]>v2[ans2],ans2=j);ans0=(ans1!=ans2);thisDiffCount+=ans0;if(ans0 && get_stdoutIt(),print1(", "j)));set_lastDiffCount(thisDiffCount);ans0}
vectDiff(v1,v2,r=min(#v1,#v2))=sum(j=1,min(#v1,min(#v2,r)),(v1[j]!=v2[j]));

/*
 * Some (perhaps useful) information about this work:
 * 

 Essentially, this script is powered with gp2c, and then from the .gp2c loading it, I generate the b-files.
 
 In short, either this or the gp2c compilationmight be used, but then an additional script is used. One that
 simply loads the compiled items, and stand by for us.
 
 Well... this is about the review of matching values between the two variations of the proposed sequence,
 the detection of maximums among the first terms up to certain limit, and the count of differences for those
 maximus.
 
bash-4.2$ /usr/bin/gcc -c -o newseq_zeros.txt.o -O3 -Wall -fno-strict-aliasing -fomit-frame-pointer -fPIC -I"/usr/local/include" newseq_zeros.txt.c && /usr/bin/gcc -o newseq_zeros.txt.so -shared -O3 -Wall -fno-strict-aliasing -fomit-frame-pointer -fPIC -Wl,-shared newseq_zeros.txt.o -lc -lm -L/usr/local/lib -lpari
bash-4.2$ $pari newseq_zeros.gp2c > bfile_zeros.txt
bash-4.2$ wc -l bfile_zeros.txt 
1000000 bfile_zeros.txt
bash-4.2$ tail bfile_zeros.txt 
7472827595086 39
7472843080859 14
7472858566642 9
7472874052443 28
7472889538250 13
7472905024087 24
7472920509930 41
7472935995779 18
7472951481636 1
7472966967499 4
bash-4.2$ cat bfile_zeros.txt | cut -d' ' -f1 > comparison_B.txt
bash-4.2$ cat bfile.txt | cut -d' ' -f1 > comparison_A.txt
bash-4.2$ head -n 1000000 bfile.txt | cut -d' ' -f1 > comparison_A.txt
bash-4.2$ md5sum comparison_*
b5e7619526142fe30834393bad60c077  comparison_A.txt
b5e7619526142fe30834393bad60c077  comparison_B.txt
bash-4.2$ head -n 1000000 bfile.txt | cut -d' ' -f2 > comparison_A.txt
bash-4.2$ head -n 1000000 bfile_zeros.txt | cut -d' ' -f2 > comparison_B.txt
bash-4.2$ clear; $pari 
? A=readvec("comparison_A.txt");
? B=readvec("comparison_B.txt");
? A[#A]
4
? B[#B]
4
? sum(j=1,#A,A[j]!=B[j])
36303
? %/#A*100+.
3.630300000000000000000000000

Around 4 percent of the terms differs 

? 593/10000*100 == 593/100
1
? 593/100 +.
5.930000000000000000000000000

Logically as expected, the percent of differences decreases tending to zero for bigger b-files. 

?
? quit();

## --- FROM ANOTHER SESSION --- [ After implementing vectDiff() ]

? A=readvec("comparison_A.txt");
? B=readvec("comparison_B.txt");
? vectDiff(A,B,10)
4
? vectDiff(A,B,100)
10
? vectDiff(A,B,1000)
76
? vectDiff(A,B,10000)
593
? vectDiff(A,B,100000)
4444
? vectDiff(A,B,1000000)
36303
?

## --- FROM ANOTHER SESSION --- [ After implementing vectmaxDiff() ]

? A=readvec("comparison_B");
  ***   at top-level: A=readvec("comparison_
  ***                   ^--------------------
  *** readvec: error opening input file: `comparison_B'.
  ***   Break loop: type 'break' to go back to GP
break> break

? A=readvec("comparison_B.txt");
? #A
1000000
? alpha=vectmax(A);
? alpha
808500
? A[%]
218

Apparently at first glance, the same maximums. But not always.

Next proposed step: (I will try it at my lab)

0) ...To generate a file with the sum of the first primes,
......for each n from 1 to fifty millions or another big size.

1) ...To use such file for sequencing both variants.

2) ...To compare the maximums and the percentages as shown above.
......Of course, for a file with 50 millions of terms, a different
......divide-and-conquer strategy becomes necessary.

3) ...Let me a little bit of time. In the middle of this week I will
......report the results. Thanks!. (On Sun Jan 26th 2014, GMT 15:20)

Meanwhile, this is about the way it works:

A=readvec("comparison_A.txt");
B=readvec("comparison_B.txt");
 
? stdout_It=0;
? vect   
vectfrq      vectmax      vectmaxDiff  vector       vectorsmall  vectorv      
? vectmaxDiff(B,B,#B)
0
? vectmaxDiff(A,B,#B)
0
? vectmaxDiff(A,B,#B\2)
0
? vectmaxDiff(A,B,#B\4)
0
? vectmaxDiff(A,B,#B\100)
0
? vectmaxDiff(A,B,#B\10)
1
? vectmaxDiff(A,B,#B\11)
1
? vectmaxDiff(A,B,#B\12)
1
? vectmaxDiff(A,B,#B\13)
0
? get_lastDiffCount()
9
? vectmaxDiff(A,B,#B)
0
? get_lastDiffCount()
128880
? vectmaxDiff(A,B,1000)
0
? get_lastDiffCount()
9
? vectmaxDiff(A,B,10)
1
? get_lastDiffCount()
9
? vectmaxDiff(A,B,20)
0
? get_lastDiffCount()
9
? vectmaxDiff(A,B,#A)
0
? get_lastDiffCount()
128880
? vectmaxDiff(A,B,125000)
1
? get_lastDiffCount()
43622
?

*/